﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Input;
using GlobalizationTools.Model;
using GlobalizationTools.UI;

namespace GlobalizationTools.ViewModel
{
    public class TreeViewResourceViewModel : TreeViewItemViewModel
    {
        readonly Resource _resource;
        readonly string _fileName;
        TabControlItemViewModel _resourceModel;

        public static RoutedUICommandEx OpenCommand = new RoutedUICommandEx(LocalizationResource.OpenResource, "OpenResource", typeof(TreeViewResourceViewModel), "/ViewModel/Images/OpenResource.png", Key.O, ModifierKeys.Control);

        public TreeViewResourceViewModel(TreeViewItemViewModel parent, Resource resource) :
            base(parent, resource.IsRootResource, false)
        {
            _resource = resource;
            if (_resource.CultureInfo == null)
            {
                _fileName = Path.GetFileName(resource.FilePath);
                _name = Path.GetFileNameWithoutExtension(resource.FilePath);
            }
            else
            {
                _fileName = Path.GetFileName(resource.FilePath);
                _name = _resource.CultureInfo.DisplayName;
            }
        }

        public TreeViewResourceViewModel(TreeViewRootViewModel parent, Resource resource, bool hasChildren) :
            base(parent, hasChildren, false)
        {
            _resource = resource;
            if (_resource.CultureInfo == null)
            {
                _fileName = Path.GetFileName(resource.FilePath);
                _name = Path.GetFileNameWithoutExtension(resource.FilePath);
            }
            else
            {
                _fileName = Path.GetFileName(resource.FilePath);
                _name = _resource.CultureInfo.DisplayName;
            }
        }

        public Resource Resource
        {
            get { return _resource; }
        }

        public string FileName
        {
            get { return _fileName; }
        }

        public override void AddInputModels(List<InputBinding> List)
        {
            base.AddInputModels(List);
            List.Add(new MouseBinding(OpenCommand, new MouseGesture(MouseAction.LeftDoubleClick)));
        }

        public override void AddCommandModels(List<CommandBinding> List)
        {
            base.AddCommandModels(List);
            List.Add(new CommandBinding(OpenCommand, new ExecutedRoutedEventHandler(ExecuteOpen)));
        }

        private void ExecuteOpen(object sender, ExecutedRoutedEventArgs e)
        {
            ShowResource();
        }

        public void ShowResource()
        {
            TreeViewRootViewModel root = this.FindRootModel();
            if (_resourceModel == null)
            {
                TreeViewResourceViewModel rootResource = this;
                if (_parent is TreeViewResourceViewModel)
                {
                    rootResource = (TreeViewResourceViewModel)_parent;
                }
                TabControlItemViewModel model = root.Resources.Where(T => T.Resource.FilePath == rootResource.Resource.FilePath).SingleOrDefault();
                if (model == null)
                {
                    _resourceModel = new TabControlItemViewModel(rootResource);
                    root.Resources.Add(_resourceModel);
                }
                else
                {
                    _resourceModel = model;
                }
            }
            if (!_resource.IsRootResource)
            {
                _resourceModel.Languages.Add(Resource);
            }
            root.SelectedTabControlModel = _resourceModel;
        }

        public TabControlItemViewModel ResourceModel
        {
            get { return _resourceModel; }
        }

        public override bool Close()
        {
            bool result;
            if (result = base.Close())
            {
                if (_resourceModel != null)
                {
                    result = _resourceModel.AskToSave();
                    if (result)
                    {
                        this.FindRootModel().Resources.Remove(_resourceModel);
                        this._resourceModel = null;
                    }
                }
            }
            return result;
        }

    }
}
